<?php

class Mobitv_Cronjobs_Reporting {

    protected $_reportDao;
    protected $_config;
    protected $_date;
    protected $_cpList;
    protected $_videoList;

    public function __construct() {
        $this->_reportDao = Vega_Dao::factory('Mobitv_Admin_Report');
        $this->_cpList = $this->_reportDao->getCpList();
        $this->_cpList[] = array('id' => 0, 'parent_id' => 0); // non-cp statistic
        $this->_config = Vega_App::getInstance()->coreConfig;
        $this->_date = date('Y-m-d');
    }

    public function updateDaily($cp=array()) {
        if ($cp) {
            $this->updateCP($cp['id'], $cp['is_subcp']);
        } else {
            foreach ($this->_cpList AS $cp) {
                // Check if this is subcp
                $isSubCp = $cp['parent_id'] ? true : false;
                $cpId = $cp['id'];

                $this->updateCP($cpId, $isSubCp);
            }
        }

        /*
         *  MONITOR STATISTIC
         */

        // VCGW statistic
        $this->_reportDao->updateDailyVcgwStatus($this->_date);

        // HLR statistic
        $this->_reportDao->updateDailyHlrStatus($this->_date);
    }

    public function updateCP($cpId, $isSubCp) {
        // Thong ke so thue bao xem VOD
        $this->_reportDao->updateDailyVodSubscriberWatchCount($this->_date, $cpId, $isSubCp);

        // Thong ke so thue bao tai VOD
        $this->_reportDao->updateDailyVodSubscriberDownloadCount($this->_date, $cpId, $isSubCp);

        // Thong ke so thue bao tang VOD
        $this->_reportDao->updateDailyVodSubscriberPresentCount($this->_date, $cpId, $isSubCp);

        // Thong ke so luot xem xem VOD
        $this->_reportDao->updateDailyVodWatchCount($this->_date, $cpId, $isSubCp);

        // Thong ke so luot truy cap VOD
        $this->_reportDao->updateDailyVodDownloadCount($this->_date, $cpId, $isSubCp);

        // Thong ke so luot tang VOD
        $this->_reportDao->updateDailyVodPresentCount($this->_date, $cpId, $isSubCp);

        // Thong ke doanh thu xem VOD
        $this->_reportDao->updateDailyVodWatchRevenue($this->_date, $cpId, $isSubCp);

        // Thong ke doanh thu tai VOD
        $this->_reportDao->updateDailyVodDownloadRevenue($this->_date, $cpId, $isSubCp);

        // Thong ke doanh thu tang VOD
        $this->_reportDao->updateDailyVodPresentRevenue($this->_date, $cpId, $isSubCp);

        // Thong ke doanh thu VOD chung
        $this->_reportDao->updateDailyVodRevenue($this->_date, $cpId, $isSubCp);

        // Thong ke theo chuyen muc VOD
        $this->_reportDao->updateDailyVodCategoryStatistic($this->_date, $cpId, $isSubCp);

        // Thong ke theo chuyen muc VOD cac video free
        $this->_reportDao->updateDailyVodCategoryStatisticFree($this->_date, $cpId, $isSubCp);

        // Thong ke tat ca luot dang ky LiveTV (ke ca free)
        if (!$cpId)
            $this->_reportDao->updateDailySubscriptionCountAll($this->_date);

        // Thong ke luot dang ky LiveTV
        if (!$cpId)
            $this->_reportDao->updateDailySubscriptionCount($this->_date);

        // Thong ke luot chuyen goi cuoc LiveTV
        if (!$cpId)
            $this->_reportDao->updateDailySwitchSubscriptionCount($this->_date);

        // Thong ke luot huy dang ky LiveTV
        if (!$cpId)
            $this->_reportDao->updateDailyUnsubscriptionCount($this->_date);

        // Thong ke luot xem LiveTV
        if (!$cpId)
            $this->_reportDao->updateDailyLivetvHitCount($this->_date);

        // Thong ke luot xem LiveTV theo kênh
        if (!$cpId)
            $this->_reportDao->updateDailyLivetvChannelHitCount($this->_date);

        // Thong ke thoi gian gian xem LiveTV
        if (!$cpId)
            $this->_reportDao->updateDailyLivetvDuration($this->_date);

        // Thong ke so thue bao xem LiveTV
        if (!$cpId)
            $this->_reportDao->updateDailyLivetvSubscriber($this->_date);

        // Thong ke thoi gian xem trung binh LiveTV
        if (!$cpId)
            $this->_reportDao->updateDailyLivetvAverageDuration($this->_date);

        // Thong ke luot dang ky cac kenh add-on
        if (!$cpId)
            $this->_reportDao->updateDailyAddonChannelSubscription($this->_date);

        // Thong ke dang ky cac kenh add-on;
        if (!$cpId)
            $this->_reportDao->updateDailyAddonChannelUnsubscription($this->_date);

        // Thong ke luot xem LiveTV theo kenh
        if (!$cpId)
            $this->_reportDao->updateDailyChannelDuration($this->_date);

        // Thong ke doanh thu dang ky dich vu LiveTV
        if (!$cpId)
            $this->_reportDao->updateDailySubscriptionRevenue($this->_date);

        // Thong ke so lan them gio dich vu LiveTV
        if (!$cpId)
            $this->_reportDao->updateDailyLivetvAddtimeCount($this->_date);

        // Thong ke doanh thu them gio
        if (!$cpId)
            $this->_reportDao->updateDailyLivetvAddtimeRevenue($this->_date);

        // Thong ke them kenh add-on
        if (!$cpId)
            $this->_reportDao->updateDailyLivetvAddonRevenue($this->_date);

        // Thong ke doanh thu tru cuoc thang
        if (!$cpId)
            $this->_reportDao->updateDailyLivetvMonfeeRevenue($this->_date);

        // Thong ke doanh thu LiveTV
        if (!$cpId)
            $this->_reportDao->updateDailyLivetvRevenue($this->_date);

        // Update top channel list
        if (!$cpId)
            $this->_reportDao->updateDailyTopChannelList($this->_date);

        // So thue bao dang ky su dung chuyen muc video
        $this->_reportDao->updateDailyVodPackageSubscribedCount($this->_date);

        // Doanh thu dang ky chuyen muc video
        $this->_reportDao->updateDailyVodPackageSubscribedRevenue($this->_date);

        // Tong so thue bao dang ky su dung dich vu ban tin video hot hang ngay
        $this->_reportDao->updateDailyVodHotSubscribedCount($this->_date);

        // Tong so thue bao huy dung dich vu ban tin video hot hang ngay
        $this->_reportDao->updateDailyVodHotUnSubscribedCount($this->_date);

        // Doanh thu ban tin video hot hang ngay
        $this->_reportDao->updateDailyVodHotRevenue($this->_date);

        // Tong so TB dang ky ban tin VOD HOT theo tuan
        $this->_reportDao->updateWeeklyVodHotSubscribe($this->_date);

        // Tong so TB huy ban tin VOD HOT theo tuan
        $this->_reportDao->updateWeeklyVodHotUnSubscribe($this->_date);

        // Tong so TB tru cuoc tuan ban tin VOD HOT theo tuan
        $this->_reportDao->updateWeeklyVodHotRevenueCount($this->_date);

        // Doanh thu dang ky moi ban tin VOD HOT theo tuan
        $this->_reportDao->updateWeeklyVodHotRevenueSubscribed($this->_date);

        // Doanh thu tru cuoc tuan ban tin VOD HOT theo tuan
        $this->_reportDao->updateWeeklyVodHotRevenue($this->_date);

        // Thue bao luy ke ban tin VOD HOT theo tuan
        $this->_reportDao->updateWeeklyVodHotTotal($this->_date);

        // Thong ke so tb dk theo cu phap CO va CO1
        $this->_reportDao->updateSubscribe3GAndMobitv($this->_date);

        //Thong ke vod tren livetv
        $this->_reportDao->updateVodChannelOnLiveTV($this->_date);
    }

    /**
     * Cap nhat lai so lieu tong so TB tru cuoc
     */
    public function updateCountMsisdnMonfee(){
        $this->_reportDao->updateCountMsisdnMonfee($this->_date);
    }

    public function updateDailyVideo() {
        // Active Users
        $this->_reportDao->updateDailyVideo($this->_date);
    }

    public function distinctUpdateDaily() {
        // Active Users
        $this->_reportDao->updateActiveUser($this->_date);
    }

    public function updateWeekly() {

    }

    public function updateMonthly() {

    }

    public function setDate($date) {
        $this->_date = $date;
    }

}

